/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __GREGSOCK_H__
#define __GREGSOCK_H__

#include <Nocopy.h>
#include <string>

namespace liba
{
namespace net
{


class LSocket : private Nocopy
{
public:
	explicit LSocket(int port); // create listening socket
	virtual ~LSocket();

	bool is_valid()const;
	bool operator!()const
	{
		return !is_valid();
	}
private:
	friend class RWSocket;

	class Impl;
	Impl * impl;
};

class RWSocket : private Nocopy
{
public:
	explicit RWSocket(const LSocket & lsock);
	RWSocket(const char * address, int port);
	virtual ~RWSocket();

	int read(void * data, int len);
	int write(const void * data, int len);
	// Read/write any data. Both functions return 
	// an actual number of bytes read/written,
	int bytes_available()const;
	// returns number of bytes, that would be read without blocking

	bool is_valid()const;
	bool operator!()const
	{
		return !is_valid();
	}
private:
	bool is_closed();
	class Impl;
	Impl * impl;			// socket handle
};

class UDPSocket : private Nocopy
{
public:
	explicit UDPSocket(int port, bool server);
	virtual ~UDPSocket();

	int broadcast(void * data, int len);
	int read(void * data, int len);
	int write(const void * data, int len);
	// Read/write any data. Both functions return 
	// an actual number of bytes read/written,
	int bytes_available()const;
	// returns number of bytes, that would be read without blocking

	std::string get_last_message_ip()const;

	bool is_valid()const;
	bool operator!()const
	{
		return !is_valid();
	}
private:
	bool is_closed();
	class Impl;
	Impl * impl;			// socket handle
	int port;

	void set_blocking(bool blocking);
};

} // namespace net
} // namespace liba
using namespace liba;

#endif